/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/
#include "zb32l03x_hal.h"
#include "log.h"
#include "app_test.h"
#include "app_key.h"
#include "Ringbuff.h"
#include "ui_lcd.h"

static decode_t decode;


void app_test_init(void)
{
  decode.decodestep = 0;
  decode.decodeitem = 0;
}
/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/

uint16_t app_test_crc_16(uint16_t usCRC, uint8_t ucDa)
{
  uint16_t uiTemp;
  uint8_t i;

  uiTemp = usCRC ^ (uint16_t)ucDa;
  for (i = 0; i < 8; i++)
  {
    if (uiTemp & 1)
    {
      uiTemp >>= 1;
      uiTemp ^= 0xA001;
    }
    else
    {
      uiTemp >>= 1;
    }
  }

  return uiTemp;
}
/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/
uint16_t app_test_crc16_cal(uint8_t *pMsg, uint16_t Len)
{
  uint16_t crc16 = 0xFFFF;
  uint16_t i;

  for (i = 0; i < Len; i++)
  {
    crc16 = app_test_crc_16(crc16, *(pMsg++));
  }
  return crc16;
}

void app_test_receviceprocess(uint8_t *buff,uint8_t len)
{
  uint8_t ucTempData, i;

  uint8_t ucCRCLo, ucCRCHi;
  uint16 ReceiveNewCRC;
  static uint8_t ucPackageLength = 0; // 用于计算CRC校验的长度

  for (i = 0; i < len; i++)
  {
    ucTempData = buff[i];
    switch (decode.decodestep)
    {
    case 0:

      if (ucTempData == SYN)
      {
        decode.decodestep = 1;
        decode.decodeitem = 0;
      }
      break;
    case 1:

      if (ucTempData != SYN)
      {
        if (ucTempData == SDLE)
        {
          decode.decodestep = 2; // 转义字符
        }
        else // 接收第一个字节
        {
          decode.decodebuff[decode.decodeitem++] = ucTempData;
          decode.decodestep = 3;
        }
      }
      break;
    case 2:
      if (ucTempData == SYNK)
      {
        decode.decodebuff[decode.decodeitem++] = SYN;
        decode.decodestep = 3;
      }
      else if (ucTempData == SDLK)
      {
        decode.decodebuff[decode.decodeitem++] = SDLE;
        decode.decodestep = 3;
      }
      else
      {
        decode.decodestep = 0; // 出错，解析下一帧
      }
      break;
    case 3:
      if (ucTempData == SDLE)
      {
        decode.decodestep = 2; // 转义字符
      }
      else if (ucTempData == SEOT) // 结束字符
      {
        decode.decodestep = 0;
        if (decode.decodeitem >= 2) // 至少有CRC两个字节
        {
          ReceiveNewCRC = app_test_crc16_cal(decode.decodebuff, decode.decodeitem - 2); // 接收后生成的CRC新值

          ucCRCLo = ReceiveNewCRC >> 0;
          ucCRCHi = ReceiveNewCRC >> 8;

          if ((ucCRCLo == decode.decodebuff[decode.decodeitem - 2]) && (ucCRCHi == decode.decodebuff[decode.decodeitem - 1]))
          {
            // 收到一包正确数据

            if (Stru_RecivePackageInfo.ucSTAMP != aucReceivePackage.ucDATA[0]) // 如果时间戳相同，说明是重发
            {
              
            }
          }
          else
          {
           // 包出错
          }
        }
        else
        {
           // 包出错
        }
      }
      else
      {
        decode.decodebuff[decode.decodeitem++] = ucTempData;
      }
      break;

    default:
      decode.decodestep = 0;
      break;
    }
  }
}